<?php
class UsersController extends Controller
{
	protected $adapter = null;
	
	public function init()
	{
		$this->adapter = V::getAdapter();
	}
	
	/**
	 * 向vo_users表插入100条记录
	 */
	public function addAction()
	{	
		for ($i=0; $i<100; $i++)
		{
			$email = '';
			switch(mt_rand(0, 3))
			{
				case 0 : $email = '@sina.com';
					break;
				case 1 : $email = '@163.com';
					break;
				case 2 : $email = '@126.com';
					break;
				case 3 : $email = '@qq.com';
					break;
			}
			
			$user = array(
				'user_loginname' => Helper::randStr(mt_rand(6, 10), 'LETTER') . '_' . $i,
				'user_password' => md5(Helper::randStr(9)),
				'group_id' => mt_rand(1, 5),
				'user_nicename' => Helper::randStr(mt_rand(6, 10), 'LETTER') . '_' . $i,
				'user_email' => Helper::randStr(mt_rand(6, 10), 'LETTER') . '_' . $i . $email,
				'dt_registered' => date('Y-m-d H:i:s', mktime() - mt_rand(0, 3600*24*30*6)),
				'ip_last_login' => mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255) . '.' . mt_rand(1, 255),
				'dt_last_login' => date('Y-m-d H:i:s', mktime() - mt_rand(0, 3600*24*30*6)),
				'user_status' => mt_rand(0, 1) === 0 ? 'n' : 'y',
			);
			
			$this->adapter->table('users')->fields($user)->insert();
			echo $this->adapter->getSql(), '<br/>';
		}
		exit;
	}
	
	/**
	 * 修改一条记录
	 */
	public function editAction()
	{
		$user = array(
			'user_loginname' => 'iphper_2',
			'user_nicename' => 'bc_2',
			'user_email' => 'iphper_2@yeah.net',
			'ip_last_login' => Helper::getIp(),
			'dt_last_login' => date('Y-m-d H:i:s'),
			'user_status' => 'y'
		);
		
		$rowCount = $this->adapter->table('users')->fields($user)->where(array('user_id = ?', 2))->update();
		echo $this->adapter->getSql(), '<br/>';
		var_dump($rowCount);
		exit;
	}
	
	/**
	 * 查找多条记录
	 */
	public function selectAction()
	{
		$rows = $this->adapter->table('users')->fields(array('user_id', 'user_loginname', 'user_nicename', 'user_email', 'ip_last_login', 'dt_last_login', 'dt_registered'))
		->where(array('user_status = ?', 'y'))->limit(10, 5)->order(array('dt_registered' => 'desc', 'dt_last_login'))->select();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($this->adapter->getParams());
		echo '<br/>';
		print_r($rows);
		exit;
	}
	
	/**
	 * 查找多条记录
	 */
	public function assocAction()
	{
		$rows = $this->adapter->table('users')->fields(array('user_id', 'user_loginname', 'user_nicename', 'user_email', 'ip_last_login', 'dt_last_login', 'dt_registered'))
		->where(array('user_status = ?', 'y'))->limit(10, 5)->order(array('dt_registered' => 'desc', 'dt_last_login'))->getAssoc();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($this->adapter->getParams());
		echo '<br/>';
		print_r($rows);
		exit;
	}
	
	/**
	 * 查找一条记录
	 */
	public function findAction()
	{
		$row = $this->adapter->table('users')->fields(array('user_loginname', 'user_nicename', 'user_email', 'ip_last_login', 'dt_last_login'))
		->where(array('user_loginname = ? AND user_email = ? AND user_status = ?', 'iphper_2', 'iphper_2@yeah.net', 'y'))->find();
		
		echo '<pre>';
		echo $this->adapter->getSql(), '<br/>';
		print_r($this->adapter->getParams());
		echo '<br/>';
		print_r($row);
		exit;
	}
	
} 
